#include <stdio.h>

#include <sys/statvfs.h>

#include "sysfs.h"
#include "stat.h"

void sprintbar(char *buf, float per, int width)
{
    const char *barstyle  = "\u2500";
    const char *usedcolor = "\033[31m";
    const char *nocolor   = "\033[0m";

    int used = (int)(per / 100.0 * width);
    for (int i = 0; i < width; i++) {
        if (used && i == 0) {
            strcat(buf, usedcolor);
        }
        strcat(buf, barstyle);
        if (used && (i == used || used >= width)) {
            strcat(buf, nocolor);
        }
    }
}


int sbar(char *buf, float per, int width)
{
    const char *usedbar = "/";
    const char *nobar   = " ";

    int used = (int)(per / 100.0 * width);

    for (int i = 0; i < width; i++) {
        if (i < used) {
            strcat(buf, usedbar);
        } else {
            strcat(buf, nobar);
        }
    }

    // buf[width - 1] = '|';
    return strlen(buf);
}


char *sbar2(char *buf, float per, int width)
{
    const char taken = '/';
    const char blank = ' ';

    int used = (int)(per / 100.0 * (width - 2));

    memset(buf, blank, width);
    buf[0] = '[';
    buf[width - 1] = ']';
    memset(buf + 1, taken, used);

    return strlen(buf);
}


void show_cpu(void)
{
    static cpu_t cpu[CPU_MAX] = {0};
    char buf[4 * 1024] = {0};
    int  len;

    printf("CPU\n");
    int cpu_num = get_cpu(cpu);
    for (int i = 0; i < cpu_num/2; i++) {
        len = 0;
        memset(buf, 0, sizeof(buf));
        // len += sprintf(buf + len, "[%2d# %5.1f%%] ", i + 1, cpu[i].usage);
        len += sprintf(buf + len, "%2d [", i + 1);
        len += sbar(buf + len, cpu[i].usage, 40);
        len += sprintf(buf + len, "%5.1f%%] ", cpu[i].usage);

        //len += sprintf(buf + len, "%5.1f%%] %2d [", (i + 1) + cpu_num/2, cpu[i + cpu_num/2].usage);
        len += sprintf(buf + len, " %2d [", (i + 1) + cpu_num/2);
        len += sbar(buf + len, cpu[i+cpu_num/2].usage, 40);
        len += sprintf(buf + len, "%5.1f%%] ", cpu[i+cpu_num/2].usage);
        printf("%s\n", buf);
    }

}

void show_fs(char *dir)
{
    char buf[1024] = {0};
    int len = 0;

    struct statvfs stat;

    // 获取文件系统信息
    if (statvfs(dir, &stat) == -1) {
        perror("statvfs");
        return;
    }

    uint64_t total = stat.f_blocks * stat.f_frsize;
    uint64_t free  = stat.f_bfree * stat.f_frsize;
    uint64_t used  = total - free;

    printf("FS\n");
    float usage = (float)(used * 100.0 / total);
    len += sprintf(buf + len, " %s [", dir);
    len += sbar(buf + len, usage, 40);
    len += sprintf(buf + len, "%5.1f%%]", usage);
    printf("%s\n", buf);
    printf(" used  %16lu B %16lu K %16lu M\n", used, used/1024, used/1024/1024);
    printf(" free  %16lu B %16lu K %16lu M\n", free, free/1024, free/1024/1024);
    printf("total  %16lu B %16lu K %16lu M\n", total, total/1024, total/1024/1024);

}

int main(int argc, char *argv[])
{
    cpu_t cpu[CPU_MAX] = {0};

    printf("\033[2J");

    char buf[4 * 1024] = {0};
    int len;

    while (1) {
        printf("\033[H\033[J");
        show_cpu();
        printf("\n");
        show_fs("/");
        sleep(1);
    }
}
